<?php

namespace App\Http\Requests;

use Carbon\Carbon;
use Illuminate\Validation\Rule;

class ReceiveCouponCodeRequest extends BaseRequest
{
    public function rules()
    {
        return [
            'coupon_code_id' => [
                'required',
                // 判断优惠券是否存在
                Rule::exists('coupon_codes', 'id')->where(function ($query) {
                    $query->where("not_before", "<", Carbon::now())->where("not_after", ">", Carbon::now());
                }),
                // 每种优惠券只能领一张
                Rule::unique('user_coupon_codes', 'coupon_code_id')->where('user_id', $this->user()->id)
            ],
        ];
    }

    public function messages()
    {
        return [
            'exists' => '优惠券已过期',
            'unique' => '你已经领取了优惠券'
        ];
    }
}
